
//
//  TestViewController.m
//  LazyScrollViewDemo
//
//  Created by hejianghai on 2017/3/7.
//  Copyright © 2017年 taobao. All rights reserved.
//

#import "TestViewController.h"
#import "TMMuiLazyScrollView.h"
#import "SDCycleScrollView.h"
#import "testV.h"

@interface Lazy1ScrollViewCustomView : UILabel<TMMuiLazyScrollViewCellProtocol>

@end

@implementation Lazy1ScrollViewCustomView

- (void)mui_prepareForReuse
{
    NSLog(@"%@", [NSString stringWithFormat:@"%@ - Prepare For Reuse",self.text]);
}

- (void)mui_didEnterWithTimes:(NSUInteger)times
{
    NSLog(@"%@", [NSString stringWithFormat:@"%@ - Did Enter With Times - %lu",self.text,(unsigned long)times]);
}

- (void)mui_afterGetView
{
    NSLog(@"%@", [NSString stringWithFormat:@"%@ - AfterGetView",self.text]);
}
@end


@interface Lazy2ScrollViewCustomView : SDCycleScrollView<TMMuiLazyScrollViewCellProtocol>

@end

@implementation Lazy2ScrollViewCustomView

- (void)mui_prepareForReuse
{
    NSLog(@"1");
}

- (void)mui_didEnterWithTimes:(NSUInteger)times
{
    NSLog(@"11");
}

- (void)mui_afterGetView
{
    NSLog(@"111");
}
@end



@interface Lazy3ScrollViewCustomView : SDCycleScrollView<TMMuiLazyScrollViewCellProtocol>

@end

@implementation Lazy3ScrollViewCustomView

- (void)mui_prepareForReuse
{
    NSLog(@"2");
}

- (void)mui_didEnterWithTimes:(NSUInteger)times
{
    NSLog(@"22");
}

- (void)mui_afterGetView
{
    NSLog(@"222");
}
@end





@interface Lazy4ScrollViewCustomView : UIView<TMMuiLazyScrollViewCellProtocol>
@property(nonatomic,strong)UIImageView *imv;
@property(nonatomic,strong)UILabel  *lab;
@end

@implementation Lazy4ScrollViewCustomView
- (void)mui_prepareForReuse
{
    NSLog(@"3");
    _imv.image=nil;
    int y =1+(arc4random() % 6);
    NSLog(@"%d",y);
    UIImage  *img=[UIImage imageNamed:[NSString stringWithFormat:@"cycle_image%d",y]];
    self.imv.image=img;
}

- (void)mui_didEnterWithTimes:(NSUInteger)times
{
    NSLog(@"33");
}

- (void)mui_afterGetView
{
    NSLog(@"333");
}

@end

//@interface SDV : SDCycleScrollView
//@end

//@implementation SDV
//
//- (void)mui_prepareForReuse
//{
////    NSLog(@"%@", [NSString stringWithFormat:@"%@ - Prepare For Reuse",self.text]);
//}
//
//- (void)mui_didEnterWithTimes:(NSUInteger)times
//{
////    NSLog(@"%@", [NSString stringWithFormat:@"%@ - Did Enter With Times - %lu",self.text,(unsigned long)times]);
//}
//
//- (void)mui_afterGetView
//{
////    NSLog(@"%@", [NSString stringWithFormat:@"%@ - AfterGetView",self.text]);
//}
//@end

@interface TestViewController ()<TMMuiLazyScrollViewDataSource,SDCycleScrollViewDelegate>
@property(nonatomic,strong)NSMutableArray  *imageArr;
@property(nonatomic,strong)NSMutableArray   *rectArray;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.imageArr=[NSMutableArray new];
    self.rectArray=[NSMutableArray new];
    
    for (int i=1; i<7; i++) {
        UIImage  *img=[UIImage imageNamed:[NSString stringWithFormat:@"cycle_image%d",i]];
        [self.imageArr addObject:img];
    }
    
    
    TMMuiLazyScrollView   *scrollview=[[TMMuiLazyScrollView alloc]init];
    scrollview.frame=self.view.bounds;
    scrollview.dataSource=self;
    [self.view addSubview:scrollview];
    
    
    
    [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(0, 0 , self.view.bounds.size.width, 210)]];
    
     [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(0, 210 , self.view.bounds.size.width, 60)]];
    //Create a double column layout with 10 elements;
    for (int i = 0; i < 4 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%4)*self.view.bounds.size.width/4 + 3, 270  , self.view.bounds.size.width/4 -3, 100 - 2)]];
    }
    [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(3, 380 , self.view.bounds.size.width -6, 120 - 2)]];
    for (int i = 0; i < 3 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%3)*self.view.bounds.size.width/3 + 3, 500  , self.view.bounds.size.width/3 -3, 180 - 2)]];
    }
    
    for (int i = 0; i < 4 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%4)*self.view.bounds.size.width/4 + 3, 680 , self.view.bounds.size.width/4 -3, 120 - 2)]];
    }
    
    [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(3, 800 , self.view.bounds.size.width -6, 120 - 2)]];
    
    for (int i = 0; i < 3 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%3)*self.view.bounds.size.width/3 + 3, 920  , self.view.bounds.size.width/3 -3, 180 - 2)]];
    }
    
    //Create a trible column layout with 15 elements;
    for (int i = 0; i < 9 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%3)*self.view.bounds.size.width/3 + 1, 1100 + i/3 *100 + 2 , self.view.bounds.size.width/3 -3, 100 - 2)]];
    }
    for (int i = 0; i < 6 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%2)*self.view.bounds.size.width/2 + 1, 1400 + i/2 *100 + 2 , self.view.bounds.size.width/2 -3, 100 - 2)]];
    }
    
    
    [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(0, 1700 , self.view.bounds.size.width/3 -3, 100 )]];
    for (int i=0; i<3;i++) {
         [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(self.view.bounds.size.width/3+(i%3)*self.view.bounds.size.width/3*2/3 + 1, 1700  , self.view.bounds.size.width/3*2/3-3, 100 )]];
    }
   
    for (int i=0; i<3;i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%3)*self.view.bounds.size.width/3*2/3 + 1, 1800  , self.view.bounds.size.width/3*2/3-3, 100)]];
    }
     [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake(self.view.bounds.size.width/3*2, 1800 , self.view.bounds.size.width/3 -3, 100)]];
    
    for (int i = 0; i < 2 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%2)*self.view.bounds.size.width/2 + 3, 1900 , self.view.bounds.size.width/2 -3, 80 - 2)]];
    }
    [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake( 3, 1980 , self.view.bounds.size.width -3, 140 - 2)]];
    for (int i = 0; i < 3 ; i++) {
        [self.rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%3)*self.view.bounds.size.width/3 + 3, 2120 , self.view.bounds.size.width/3 -3, 120 - 2)]];
    }
    scrollview.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds), 2240);
    //STEP 3 reload LazyScrollView
    [scrollview reloadData];

    // Do any additional setup after loading the view.
}

- (NSUInteger)numberOfItemInScrollView:(TMMuiLazyScrollView *)scrollView
{
    return self.rectArray.count;
}

- (TMMuiRectModel *)scrollView:(TMMuiLazyScrollView *)scrollView rectModelAtIndex:(NSUInteger)index
{
    CGRect rect = [(NSValue *)[self.rectArray objectAtIndex:index]CGRectValue];
    TMMuiRectModel *rectModel = [[TMMuiRectModel alloc]init];
    rectModel.absoluteRect = rect;
    rectModel.muiID = [NSString stringWithFormat:@"%ld",index];
    return rectModel;
}

- (UIView *)scrollView:(TMMuiLazyScrollView *)scrollView itemByMuiID:(NSString *)muiID
{
    
      NSInteger index = [muiID integerValue];
    NSLog(@"%ld",(long)index);
    if (index==0) {
        SDCycleScrollView  *sdv=(SDCycleScrollView *)[scrollView dequeueReusableItemWithIdentifier:@"sdv"];
        if (!sdv) {
            sdv=[SDCycleScrollView cycleScrollViewWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue] imageNamesGroup:_imageArr];
            sdv.reuseIdentifier = @"sdv";
            sdv.titlesGroup=@[@"1你好世界那么大1",@"2你好世界那么大你好世界那么大你好世界那么大你好世界那么大你好世界那么大你好世界那么大你好世界那么大你好世界那么大2",@"3你好世界那么大3",@"4你好世界那么大4",@"5你好世界那么大5",@"6你好世界那么大6"];
            sdv.pageControlAliment=SDCycleScrollViewPageContolAlimentRight;
//            sdv.titleLabelHeight=60;
        }
          sdv.frame = [(NSValue *)[self.rectArray objectAtIndex:index]CGRectValue];
        [scrollView addSubview:sdv];
        return sdv;
    }
    else if (index==1)
    {
        Lazy3ScrollViewCustomView  *textSv=(Lazy3ScrollViewCustomView *)[scrollView dequeueReusableItemWithIdentifier:@"textSv"];
        if (!textSv) {
            textSv=[Lazy3ScrollViewCustomView cycleScrollViewWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue] delegate:self placeholderImage:nil];
            textSv.reuseIdentifier = @"textSv";
            textSv.titleLabelBackgroundColor=[UIColor whiteColor];
            textSv.titleLabelTextColor=[UIColor blackColor];
            textSv.scrollDirection = UICollectionViewScrollDirectionVertical;  //设置滚动方向
            textSv.onlyDisplayText = YES;     //只显示文字
            
            NSMutableArray *titlesArray = [NSMutableArray new];
            [titlesArray addObject:@"纯文字上下滚动轮播sadkjsdask\nh发dffdasgd啊身高的健\n康尴尬死的事噶看到嘎斯哥萨克蛋糕dffdfdddfdfd"];
            [titlesArray addObject:@"测试Demo1"];
            [titlesArray addObject:@"测试Demo2"];
            textSv.titlesGroup = [titlesArray copy];
        }
   
        textSv.frame = [(NSValue *)[self.rectArray objectAtIndex:index]CGRectValue];
        [scrollView addSubview:textSv];
        return textSv;
    }
    else if ((index>=2&&index<10))
    {
        Lazy4ScrollViewCustomView  *imgV=(Lazy4ScrollViewCustomView *)[scrollView dequeueReusableItemWithIdentifier:@"imgV"];
        if (!imgV) {
            imgV=[[Lazy4ScrollViewCustomView alloc]initWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue]];
            UIImage  *img=[UIImage imageNamed:@"cycle_image1"];
            imgV.imv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imgV.frame.size.width, imgV.frame.size.height-20)];
            imgV.imv.image=img;
            imgV.lab=[[UILabel alloc]initWithFrame:CGRectMake(0, imgV.frame.size.height-20, imgV.frame.size.width, 20)];
            imgV.lab.backgroundColor=[UIColor whiteColor];
            imgV.lab.textColor=[UIColor redColor];
            imgV. lab.text=@"123456789";
            [imgV addSubview: imgV.imv];
            [imgV addSubview: imgV.lab];
            imgV.reuseIdentifier=@"imgV";
        }
      
        imgV.frame=[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue];
        imgV.imv.frame=CGRectMake(0, 0, imgV.frame.size.width, imgV.frame.size.height-20);
        imgV.lab.frame=CGRectMake(0, imgV.frame.size.height-20, imgV.frame.size.width, 20);
        [scrollView addSubview:imgV];
        return imgV;
    }
    else if (index>=41&&index<=46)
    {
        testV  *test=(testV *)[scrollView dequeueReusableItemWithIdentifier:@"test"];
        if (!test) {
            test=[[testV alloc]initWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue]];
            test.reuseIdentifier=@"test";
        }
        test.frame=[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue];
          NSLog(@"%ld   %f   %f   %f   %f",(long)index,test.frame.size.width,test.frame.size.height,test.frame.origin.x,test.frame.origin.y);
        test.imv.frame=CGRectMake(0, 20, test.frame.size.width, test.frame.size.height-20);
        test.lab.frame=CGRectMake(0, 0, test.frame.size.width, 20);
        [scrollView addSubview:test];
        return test;
        
    }
    else if (index==33||index==40)
    {
        
        UIView  *v=(UIView *)[scrollView dequeueReusableItemWithIdentifier:@"V"];
        if (!v) {
            v=[[UIView alloc]initWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue]];
            UIImage  *img=[UIImage imageNamed:@"cycle_image2"];
            UIImageView  *imv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, v.frame.size.width-30, v.frame.size.height)];
            imv.image=img;
            UILabel  *lab=[[UILabel alloc]initWithFrame:CGRectMake(v.frame.size.width-30, 0,30, v.frame.size.height)];
            lab.backgroundColor=[UIColor whiteColor];
            lab.textColor=[UIColor redColor];
            lab.text=@"我";
            [v addSubview:imv];
            [v addSubview:lab];
            v.reuseIdentifier=@"v";
        }
        v.frame = [(NSValue *)[self.rectArray objectAtIndex:index]CGRectValue];
        [scrollView addSubview:v];
        return v;
        
    }
    else if ((index>=34&&index<37)||(index>=37&&index<40))
    {
        UIView  *v=(UIView *)[scrollView dequeueReusableItemWithIdentifier:@"V"];
        if (!v) {
            v=[[UIView alloc]initWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue]];
            UIImage  *img=[UIImage imageNamed:@"cycle_image3"];
            UIImageView  *imv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, v.frame.size.width, v.frame.size.height-20)];
            imv.image=img;
            UILabel  *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,v.frame.size.width , 20)];
            lab.backgroundColor=[UIColor whiteColor];
            lab.textColor=[UIColor redColor];
            lab.text=@"你好世界";
            [v addSubview:imv];
            [v addSubview:lab];
            v.reuseIdentifier=@"v";
        }
         v.frame = [(NSValue *)[self.rectArray objectAtIndex:index]CGRectValue];
        [scrollView addSubview:v];
        return v;
    }
    else
    {
    //Find view that is reuseable first.
    Lazy1ScrollViewCustomView *label = (Lazy1ScrollViewCustomView *)[scrollView dequeueReusableItemWithIdentifier:@"testView"];
  
    if (!label)
    {
        label = [[Lazy1ScrollViewCustomView alloc]initWithFrame:[(NSValue *)[_rectArray objectAtIndex:index]CGRectValue]];
        label.textAlignment = NSTextAlignmentCenter;
        label.reuseIdentifier = @"testView";
    }
    label.frame = [(NSValue *)[_rectArray objectAtIndex:index]CGRectValue];
    label.text = [NSString stringWithFormat:@"%lu",(unsigned long)index];
    label.backgroundColor = [self randomColor];
    [scrollView addSubview:label];
    label.userInteractionEnabled = YES;
    [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)]];
    return label;
    }
    return nil;
}



- (UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    
}

- (void)click:(UIGestureRecognizer *)recognizer
{
    Lazy1ScrollViewCustomView *label = (Lazy1ScrollViewCustomView *)recognizer.view;
    
    NSLog(@"Click - %@",label.muiID);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
